package top.psjj.ssm.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.psjj.ssm.entity.User;
import top.psjj.ssm.mapper.UserMapper;
import top.psjj.ssm.service.UserService;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;


    public User login(User user) {
        User temp = userMapper.login(user);
        System.out.println(temp);
        return temp;
    }

    public List<User> list(User user) {
        List<User> list = new ArrayList<User>();
        if (user==null || user.getUsername() == null || user.getUsername().length() == 0){
            return userMapper.list();
        }
        list.add(userMapper.selectByUsername(user));
        return list;
    }

    public User selectByUsername(User user) {
        return userMapper.selectByUsername(user);
    }

    public User selectById(Integer id) {
        return userMapper.selectById(id);
    }

    public void save(User user) {
        User temp = userMapper.selectById(user.getId());
        if (temp == null){
            //添加操作
            user.setPassword("123");
            userMapper.insertUser(user);
        }else{
            //修改操作
            userMapper.updateById(user);
        }
    }

    public void deleteById(Integer id) {
        userMapper.deleteById(id);
    }
}
